﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstraX.BuildWizard
{
    public class BuildOperationCompletedArgs<T> : EventArgs
    {
        public T ReturnValue { get; set; }

        public BuildOperationCompletedArgs(T returnValue)
        {
            this.ReturnValue = returnValue;
        }
    }

    public class ExceptionEventArgs
    {
        public Exception Exception { get; set; }

        public ExceptionEventArgs(Exception ex)
        {
            this.Exception = ex;
        }
    }

    public delegate void BuildCompletedHandler<T>(object sender, BuildOperationCompletedArgs<T> e);
    public delegate void ExceptionHandler(object sender, ExceptionEventArgs e);

    public class BuildInvokeOperation
    {
        public event EventHandler OnBuildCompleted;
        public event ExceptionHandler ExceptionOccured;
        public DateTime BuildStarted;
        public bool IsComplete;
        public Exception Exception { get; set; }

        public BuildInvokeOperation()
        {
            this.IsComplete = false;
        }

        public BuildInvokeOperation(Exception ex)
        {
            this.Exception = ex;
            this.IsComplete = true;
        }

        public bool HasException
        {
            get
            {
                return this.Exception != null;
            }
        }

        public void SetException(Exception ex)
        {
            this.IsComplete = true;
            this.Exception = ex;
            ExceptionOccured(this, new ExceptionEventArgs(ex));
        }

        public void SetCompleted()
        {
            this.IsComplete = true;
            OnBuildCompleted(this, EventArgs.Empty);
        }
    }

    public class BuildOperation<T>
    {
        public event BuildCompletedHandler<T> Completed;
        public event ExceptionHandler ExceptionOccured;
        public Exception Exception { get; set; }

        public BuildOperation()
        {
        }

        public BuildOperation(Exception ex)
        {
            this.Exception = ex;
        }

        public bool HasException
        {
            get
            {
                return this.Exception != null;
            }
        }

        public void SetCompleted(T returnValue)
        {
            Completed(this, new BuildOperationCompletedArgs<T>(returnValue));
        }

        public void SetException(Exception ex)
        {
            this.Exception = ex;
            ExceptionOccured(this, new ExceptionEventArgs(ex));
        }
    }

    public interface IBuildProxy
    {
        void BuildMiddleLayer(string id);
        void AbortBuild(string lastUIComponentURL);
        BuildOperation<string> GetProperty(string UIComponentURL, string propertyName);
        BuildInvokeOperation SetProperty(string UIComponentURL, string propertyName, string value);
        BuildOperation<bool> MoveNext(string lastUIComponentURL);
        void Finish(string lastUIComponentURL);
    }
}
